<?php
header('Content-Type: application/json');
class sfController extends AppController
{
    private $orderId = 'QIAO-201804130054';

    /**
     * 下单
     */
    public function send_parameterAction(){
        $sfe = new SFExpressClass(true);
        $orderid = $this->orderId;
        $time = time();
        $post['shop_order_id'] = $orderid;          # 商家订单ID
        $post['order_source'] = '其他';             # 1：美团；2：饿了么；3：百度；4：口碑；其他请直接填写中文字符串值
        $post['pay_type'] = 1;                      # 付款方式  1：已付款 0：货到付款
        $post['order_time'] = $time;                # 下单时间
        $post['is_appoint'] = 0;                    # 是否预约单 0：非预约单；1：预约单
        $post['is_insured'] = 0;                    # 是否保价 0：非保价；1：保价
        $post['remark'] = '测试';                   # 非必填 订单备注  string
        $post['push_time'] = $time;                 # 推单时间
        $post['version'] = 17;                      # 版本号

        # 订单详情 obj
        $post['order_detail']['total_price'] = 200;  # 用户订单总金额
        $post['order_detail']['product_type'] = 99; # 物品类型 1快餐;2送药;3百货;4脏衣服收;5干净衣服派;6生鲜;7保单;8饮品;9现场勘查;10快递;12文件证照;13蛋糕;14鲜花;15电子数码;16服装鞋帽;17汽车配件;18珠宝;20披萨;21中餐;99其他
        $post['order_detail']['weight_gram'] = 200;  # 物品重量
        $post['order_detail']['product_num'] = 1;  # 物品个数
        $post['order_detail']['product_type_num'] = 1; # 物品种类个数

        # 物品详情 数组
        $post['order_detail']['product_detail'][0]['product_name'] = '打印纸';   # 物品名称
        $post['order_detail']['product_detail'][0]['product_num'] = 1;          # 物品数量


        # 收货人信息
        $post['receive']['user_name'] = 'Ethan';                 # 用户姓名
        $post['receive']['user_phone'] = '17321295203';          # 用户电话
        $post['receive']['user_address'] = '北京市海淀区学清路826号';        # 用户地址
        $post['receive']['user_lng'] = '116.3522490000';          # 用户经度
        $post['receive']['user_lat'] = '40.0087090000';          # 用户纬度
        $info = $sfe ->send_parameter($post);
        echo '<pre>';
        var_dump($info);
    }

    /**
     * 查询订单
     */
    public function queryAction(){
        $sfe = new SFExpressClass(true);
        $post['order_id'] = $this->orderId;
        $post['order_type'] = 2;
        $post['push_time'] = time();
        $queryInfo = $sfe ->query($post);
        echo '<pre>';
        var_dump(json_decode($queryInfo));
    }

    /**
     * 取消订单
     */
    public function cancelAction(){
        $sfe = new SFExpressClass(true);
        $post['order_id'] = $this->orderId;
        $post['order_type'] = 2;
        $post['push_time'] = time();
        $cancelInfo = $sfe ->cancel($post);
        echo '<pre>';
        var_dump($cancelInfo);
    }


    /**
     * 配送人员坐标
     */
    public function riderlatestpositionAction(){
        $sfe = new SFExpressClass(true);
        $post['order_id'] = $this->orderId;
        $post['order_type'] = 2;
        $post['push_time'] = time();
        $cancelInfo = $sfe ->riderlatestposition($post);
        echo '<pre>';
        var_dump($cancelInfo);
    }


    /**
     * 取配送人轨迹
     */
    public function riderh5viewAction(){
        $sfe = new SFExpressClass(true);
        $post['order_id'] = $this->orderId;
        $post['order_type'] = 2;
        $post['push_time'] = time();
        $cancelInfo = $sfe ->riderh5view($post);
        echo '<pre>';
        var_dump($cancelInfo);
    }

    /**
     * 订单回调详情
     * http://106.14.72.80/?md=index＆cl=sf＆at=getcallbackinfo
     */
    public function getcallbackinfoAction(){
        $sfe = new SFExpressClass(true);
        $post['order_id'] = $this->orderId;
        $post['order_type'] = 2;
        $post['push_time'] = time();
        $cancelInfo = $sfe ->getcallbackinfo($post);
        echo '<pre>';
        var_dump($cancelInfo);
    }

    /**
     * 顺丰取消原因   顺丰主动回调
     */
    public function sfcancelAction(){
        $body = file_get_contents('php://input');
        file_put_contents('logs/sfcancel',$body);
    }


    /**
     * 订单状态回调
     * 10-配送员确认;12:配送员到店;15:配送员配送中
     */
    public function getcallbackstatusAction(){
        $body = file_get_contents('php://input');
        file_put_contents('logs/SFgetcallbackstatus',"$body".PHP_EOL,FILE_APPEND);
        $returnInfo = json_decode($body,true);
        if($returnInfo['order_status'] == 10 || $returnInfo['order_status'] == 12 || $returnInfo['order_status'] == 15){
            $return = ["error_code"=>0,"error_msg"=>"success"];
            echo  json_encode($return);
        }
    }

    /**
     * 订单完成回调
     * 17配送员点击完成
     */
    public function getcallbackfinishAction(){
        $body = file_get_contents('php://input');
        file_put_contents('logs/SFgetcallbackfinish',"$body".PHP_EOL,FILE_APPEND);
        $returnInfo = json_decode($body,true);
        if($returnInfo['order_status'] == 17){
            $return = ["error_code"=>0,"error_msg"=>"success"];
            echo  json_encode($return);
        }
    }

}
